// pages/upload-pic/upload-pic.js
Page({

  data: {
    images: [],
    imgs: [],
    tempImg: ''
  },

  onLoad: function (options) {

  },

  // 图片操作的具体函数
  ImageOperator() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res.tempFilePaths[0])
        // 上传的图片数据
        let imgList = res.tempFilePaths;
        // 原始的图片数据
        let imageList = this.data.images;
        // 原来的图片数量
        let imageLength = imageList.length;
        // 当前的图片数量
        let nowLength = imgList.length;
        if (imageLength < 5) {
          let images = [];
          // 获取缺少的图片张数
          let residue = 5 - imageLength;
          // 如果缺少的张数大于当前的的张数  
          if (residue >= nowLength) {
            // 直接将两个数组合并为一个  
            images = imageList.concat(imgList);
          } else {
            // 否则截取当前的数组一部分  
            images = imageList.concat(imgList.slice(0, residue));
          }
          this.setData({
            images,
            tempImg: res.tempFilePaths[0]
          })
        }
      }
    })
  },
  // 图片获取
  chooseImage() {
    this.ImageOperator()
  },
  // 删除图片
  deleteImage(event) {
    //获取数据绑定的data-id的数据
    const nowIndex = event.currentTarget.dataset.id;
    let images = this.data.images;
    images.splice(nowIndex, 1);
    this.setData({
      images
    })
  },
  // 预览图片
  previewIamge(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const images = this.data.images;
    wx.previewImage({
      current: images[nowIndex],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },

  formSubmit(e){
    let {id_card, realname} = e.detail.value
    let access_token = wx.getStorageSync('access_token')
    if(e.detail.value.realname == '') {
      wx.showToast({
        title: '请输入姓名！',
        icon:'none'
      })
      return false
    }
    if(e.detail.value.id_card == '') {
      wx.showToast({
        title: '请输入身份证号！',
        icon:'none'
      })
      return false
    }
    if(this.data.images.length == 0){
      wx.showToast({
        title: '请上传照片！',
        icon:'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在上传...',
    })
    // promise上传表单
    let promiseArr = []
    let promise = new Promise((resolve, reject) => {
      wx.uploadFile({
        filePath: this.data.tempImg,
        name: 'file',
        url: 'https://retire.wshoto.com/wx/imageupload',
        complete: (res) => {
          console.log(res)
          let resArr = JSON.parse(res.data).url
          this.setData({
            imgs: resArr
          })
          resolve(res)
        }
      })
    })
    promiseArr.push(promise)
    Promise.all(promiseArr).then(res => {
      let photo = this.data.imgs
      wx.request({
        url: 'https://retire.wshoto.com/wx/faceidentify',
        data: {
          access_token: access_token,
          photo: photo,
          realname:realname,
          id_card:id_card
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if(res.data.code == 1){
            wx.switchTab({
              url: '../index/index',
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel:false,
              success: (res) => {
                console.log(res)
              }
            })
          }
        }
      })
    })
  }
})